<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\ding;

use app\api\service\User as UserService;
use app\common\model\ding\CouponCategory as CouponCategoryModel;
use think\facade\Db;

/**
 * 商品评价模型
 * Class Article
 * @package app\api\model
 */
class CouponCategory extends CouponCategoryModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'store_id',
        'create_time',
        'update_time',
    ];

    public function add(int $shopId, $data)
    {
        if (isset($data['categoryId'])) {
            Db::name('ding_food_category')->where('category_id', $data['categoryId'])
                ->update(['name' => $data['name'], 'sort' => $data['sort']]);
            Db::name('ding_food')->where('category_id', $data['categoryId'])
                ->update(['sort' => $data['sort']]);

        } else {
            $data['shop_id'] = $shopId;

            $data['store_id'] = self::$storeId;

            $this->save($data);
        }

        return true;
    }
    /**
     * 获取文章列表
     * @param int $categoryId
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getListShop(int $shopId = 0)
    {
        // 检索查询条件
        $filter = [];

        // 获取列表数据
        return $this->with(['food.image'])
            ->where($filter)

            ->where('shop_id', '=', $shopId)
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc'])
            ->select()->toArray();
    }

    public function getListCart(int $shopId = 0)
    {
        // 获取当前用户ID
        $userId = UserService::getCurrentLoginUserId();
        // 检索查询条件
        $filter = [];

        // 获取列表数据
        $list = $this->with(['food.image'])
            ->where($filter)

            ->where('shop_id', '=', $shopId)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->select()->toArray();

        foreach ($list as $key => $value) {
            $foods = $value['food'];
            foreach ($foods as $fkey => $food) {
                $foods[$fkey]['num'] = Db::name('ding_cart')
                    ->where('food_id', $food['food_id'])
                    ->where('user_id', $userId)->value('food_num');
            }
            $list[$key]['food'] = $foods;

        }

        return $list;
    }
    public function getList(int $shopId = 0)
    {
        // 检索查询条件
        $filter = [];

        // 获取列表数据
        return $this
            ->where($filter)

            ->where('shop_id', '=', $shopId)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->select()->toArray();
    }
}
